package erupt_simultaneously;

import jdk.nashorn.internal.ir.Block;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther Jun jie Yi
 * @Date 16:40 2021/7/24
 */

public class AtomicIntegerDemo implements Runnable{

    static ReentrantLock lock = new ReentrantLock();

    //AtomicInteger 的线程安全性(两个线程打出同一个值)
    AtomicInteger atomicInteger = new AtomicInteger();
    //Integer a = 0;

    public static void main(String[] args) {

        AtomicIntegerDemo t01 = new AtomicIntegerDemo();
        new Thread(t01).start();
        new Thread(t01).start();
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            //CAS：计算出结果后会再次拿值和计算之前的值左比较是否发生变化
            System.out.println(Thread.currentThread().getName() + " : " +atomicInteger.getAndIncrement());

            //这里想要实现不重复数据只能够加锁 synchronized
            //System.out.println(Thread.currentThread().getName() + " : " + (++a));
        }
    }
}